package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.system.domain.Task;
import com.ruoyi.system.domain.TaskBak;
import com.ruoyi.system.service.impl.TaskBakServiceImpl;
import com.ruoyi.system.service.impl.TaskServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 对外白名单
 */
@RestController
@RequestMapping("/extend")
public class extendController {

    @Autowired
    private TaskServiceImpl taskService;

    @Autowired
    private TaskBakServiceImpl taskBakService;

    @Log(title = "wms推送任务")
    @PostMapping("/processTask")
    public Map<String, String> processTask(@RequestBody Map<String, Object> taskRequest) {
        Map<String, String> response = new HashMap<>();
        try {
            // 直接打印接收到的 JSON 数据
            Task task = new Task();
            task.setTaskId(taskRequest.get("TASK_ID").toString());
            task.setPalletNo(taskRequest.get("PALLET_NO").toString());
            task.setFromLoc(taskRequest.get("FROM_LOC").toString());
            task.setToLoc(taskRequest.get("TO_LOC").toString());
            task.setTaskType(taskRequest.get("TASK_TYPE").toString());
            task.setCustomer(taskRequest.get("CUSTOMER").toString());
            task.setItemCode(taskRequest.get("ITEM_CODE").toString());
            task.setItemName(taskRequest.get("ITEM_NAME").toString());
            task.setQty(Long.valueOf(taskRequest.get("QTY").toString()));
            task.setBackQty(Long.valueOf(taskRequest.get("BACK_QTY").toString()));
            task.setSts("0");
            taskService.insertTask(task);
            // 返回成功响应
            response.put("STATUS", "1");
            response.put("MSG", "成功");
        } catch (Exception e) {
            response.put("STATUS", "0");
            response.put("MSG", "失败");
        }
        return response;
    }


    @Log(title = "wms取消任务")
    @PostMapping("/delTask")
    public Map<String, String> delTask(@RequestBody Map<String, Object> taskRequest) {
        Map<String, String> response = new HashMap<>();
        try {
            // 直接打印接收到的 JSON 数据
            Task task = new Task();
            task.setTaskId(taskRequest.get("TASK_ID").toString());
            task.setPalletNo(taskRequest.get("PALLET_NO").toString());
            List<Task> tasks = taskService.selectTaskList(task);
            for (Task task1 : tasks) {
                if (task1.getSts().equals("1")){
                    response.put("STATUS", "0");
                    response.put("MSG", "任务已处理不能取消");
                    return response;
                }
                TaskBak taskBak = new TaskBak();
                BeanUtils.copyProperties(task1, taskBak);
                taskBak.setInfo(taskRequest.get("REMARK").toString());
                //写入bak
                taskBakService.insertTaskBak(taskBak);
            }
            // 返回成功响应
            response.put("STATUS", "1");
            response.put("MSG", "成功");
        } catch (Exception e) {
            response.put("STATUS", "0");
            response.put("MSG", "失败");
        }
        return response;
    }


}
